package com.farm.shop.controller;

import cn.hutool.core.convert.Convert;
import com.farm.common.annotation.Log;
import com.farm.common.utils.PageUtils;
import com.farm.common.utils.Query;
import com.farm.common.utils.R;
import com.farm.shop.constant.PageParam;
import com.farm.shop.domain.FarmShopDO;
import com.farm.shop.domain.ScenicDO;
import com.farm.shop.service.ScenicService;
import com.farm.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 景区
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-09-15 08:56:11
 */
@Slf4j
@Controller
@RequestMapping("/farm/scenic")
public class ScenicController {
    @Autowired
    private ScenicService scenicService;

    /**
     * app 获取景区详情
     *
     * @param params 请求参数
     * @return Result
     */
    @Log(value = "A获取景区详情")
    @ResponseBody
    @GetMapping("/app/scenicdetail")
    public Result getScenicinforsDetail(@RequestParam Map<String, Object> params) {

        Integer scenicId = Convert.convert(Integer.class, params.get("scenicId"));
        ScenicDO appScenicDetail = scenicService.get(scenicId);
        return Result.success(appScenicDetail);
    }

    /**
     * app 重点景区 分页
     *
     * @param params 请求参数
     * @return Result
     */
    @Log(value = "A重点景区列表")
    @ResponseBody
    @GetMapping("/app/emphasisscenic")
    public Result getScenicinfors(@RequestParam Map<String, Object> params) {
        int currPage = Integer.parseInt((String) params.get("page"));
        params.put("offset", (currPage - 1) * PageParam.LIMIT);
        params.put("limit", PageParam.LIMIT);

        Query query = new Query(params);

        List<ScenicDO> appScenics = scenicService.list(query);
        int total = scenicService.count(query);
        PageUtils pageUtils = new PageUtils(total, currPage, appScenics);
        return Result.success(pageUtils);
    }

    @GetMapping()
    @RequiresPermissions("farm:scenic:scenic")
    String Scenic() {
        return "farm/scenic/scenic";
    }

    @Log(value = "B查看景区地址")
    @GetMapping("/getLocation/{id}")
    String getLocation(@PathVariable("id") Integer id, Model model) {
        ScenicDO scenic = scenicService.get(id);
        log.info(scenic.toString());
        model.addAttribute("address", scenic.getAddress());
        model.addAttribute("lng", scenic.getLng());
        model.addAttribute("lat", scenic.getLat());
        return "farm/scenic/scenicAddress";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("farm:scenic:scenic")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ScenicDO> scenicList = scenicService.list(query);
        int total = scenicService.count(query);
        PageUtils pageUtils = new PageUtils(scenicList, total);
        return pageUtils;
    }

    @Log(value = "B模糊查询景区列表")
    @ResponseBody
    @GetMapping("/searchName")
    @RequiresPermissions("farm:scenic:scenic")
    public PageUtils searchName(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ScenicDO> scenicList = scenicService.searchName(query);
        int total = scenicService.searchNameCount(params);
        PageUtils pageUtils = new PageUtils(scenicList, total);
        return pageUtils;
    }

    @GetMapping("/hotScenic/page")
    @RequiresPermissions("farm:scenic:hotScenic")
    String HotScenic() {
        return "farm/scenic/hotScenic";
    }

    @ResponseBody
    @GetMapping("/hotScenic")
    @RequiresPermissions("farm:scenic:scenic")
    public PageUtils hotScenic(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ScenicDO> scenicList = scenicService.list(query);
        int total = scenicService.count(query);
        PageUtils pageUtils = new PageUtils(scenicList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("farm:scenic:add")
    String add() {
        return "farm/scenic/add";
    }

    @GetMapping("/address")
    String address() {
        return "farm/scenic/address";
    }

    @GetMapping("/edit/{scenicid}")
    @RequiresPermissions("farm:scenic:edit")
    String edit(@PathVariable("scenicid") Integer scenicid, Model model) {
        ScenicDO scenic = scenicService.get(scenicid);
        model.addAttribute("scenic", scenic);
        return "farm/scenic/edit";
    }

    @GetMapping("/hotPreview/{scenicid}")
    @RequiresPermissions("farm:scenic:edit")
    String hotPreview(@PathVariable("scenicid") Integer scenicid, Model model) {
        ScenicDO scenic = scenicService.get(scenicid);
        model.addAttribute("scenic", scenic);
        return "farm/scenic/hotPreview";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("farm:scenic:add")
    public R save(ScenicDO scenic) {
        if (scenicService.save(scenic) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("farm:scenic:edit")
    public R update(ScenicDO scenic) {
        scenicService.update(scenic);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("farm:scenic:remove")
    public R remove(Integer scenicid) {
        if (scenicService.remove(scenicid) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("farm:scenic:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] scenicids) {
        scenicService.batchRemove(scenicids);
        return R.ok();
    }

}
